package com.soft.sitegenerator.action;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.NewsGroup;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.NewsGroupEditForm;

public class NewsGroupEditAction extends DispatchAction {

	public ActionForward editNewsGroup(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsGroupEditForm newsGroupEditForm = (NewsGroupEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		NewsGroup newsGroup = newsGroupEditForm.getNewsGroup();
		newsGroup.setId(id);

		try {
			newsGroup.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		newsGroupEditForm.setNewsGroup(newsGroup);

		
		// Obtengo el usuario que genero el grupo
		Component component = new DataBaseComponent();
		User usuario = (User) component.getBean("usersHib");

		newsGroupEditForm.setUsuarios(usuario.findAll());
		if (newsGroup.getUser() != null)
			newsGroupEditForm.setIdUser(newsGroup.getUser().getId());

		return mapping.findForward("showEdit");
	}

	public ActionForward deleteNewsGroup(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		NewsGroupEditForm newsGroupEditForm = (NewsGroupEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		NewsGroup newsGroup = newsGroupEditForm.getNewsGroup();
		newsGroup.setId(id);
		newsGroup.delete();

		return mapping.findForward("showList");
	}

	public ActionForward addNewsGroup(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		NewsGroupEditForm formEdit = (NewsGroupEditForm) form;

		// Obtengo los usuarios
		Component component = new DataBaseComponent();
		User usuario = (User) component.getBean("usersHib");
		formEdit.setUsuarios(usuario.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveNewsGroup(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NewsGroupEditForm newsGroupEditForm = (NewsGroupEditForm) form;

		NewsGroup grupo = newsGroupEditForm.getNewsGroup();

		// Obtengo el usuario
		Component component = new DataBaseComponent();
		User usuario = (User) component.getBean("usersHib");

		if (newsGroupEditForm.getIdUser().longValue()!=0) {
			usuario.setId(newsGroupEditForm.getIdUser());

			try {
				usuario.load();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			}

			grupo.setUser(usuario);
			usuario.getNewsGroups().add(grupo);
		} else {
			grupo.setUser(null);
		}

		grupo.setCreationDate(new Date());
		grupo.save();

		return mapping.findForward("showList");

	}

	public ActionForward updateNewsGroup(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		NewsGroupEditForm newsGroupEditForm = (NewsGroupEditForm) form;
		NewsGroup grupo = newsGroupEditForm.getNewsGroup();

		// Obtengo el usuario
		Component component = new DataBaseComponent();
		User usuario = (User) component.getBean("usersHib");

		if (newsGroupEditForm.getIdUser().longValue()!=0) {
			usuario.setId(newsGroupEditForm.getIdUser());

			try {
				usuario.load();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			}

			grupo.setUser(usuario);
			usuario.getNewsGroups().add(grupo);
		} else {
			grupo.setUser(null);
		}

		grupo.setCreationDate(new Date());
		
		grupo.update();

		return mapping.findForward("showList");
	}
}